package com.atguigu.ggkt.vod.controller;


import com.atguigu.ggkt.model.vod.Course;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vo.vod.CourseFormVo;
import com.atguigu.ggkt.vo.vod.CoursePublishVo;
import com.atguigu.ggkt.vo.vod.CourseQueryVo;
import com.atguigu.ggkt.vod.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Jetwang
 * @since 2022-06-30
 */
@RestController
@RequestMapping("/admin/vod/course")
@Api(tags = "课程管理接口")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("findAll")
    public Result findAll() {
        List<Course> list = courseService.findlist();
        return Result.ok(list);
    }

    /**
     * 获取点播课程列表
     * @param current 当前页
     * @param limit 页码大小
     * @param courseQueryVo 查询条件
     * @return
     */
    @ApiOperation("获取点播课程列表")
    @PostMapping("courses/{current}/{limit}")
    public Result listCourses(@PathVariable Long current,
                              @PathVariable Long limit,
                              @RequestBody(required = false) CourseQueryVo courseQueryVo) {
        Page<Course> page = new Page<>(current, limit);
        Map<String, Object> result = courseService.findPageCourse(page, courseQueryVo);
        return Result.ok(result);
    }

    /**
     * 添加课程基本信息
     * @param courseFormVo 课程json格式信息
     * @return 返回课程id
     */
    @ApiOperation("添加课程基本信息")
    @PostMapping("course")
    public Result saveCourse(@RequestBody CourseFormVo courseFormVo) {
        Long courseId = courseService.saveCourseInfo(courseFormVo);
        return Result.ok(courseId);
    }

    /**
     * 根据id获取课程信息
     * @param id 课程id
     * @return
     */
    @ApiOperation("根据id获取课程信息")
    @GetMapping("course/{id}")
    public Result getCourseById(@PathVariable Long id) {
        CourseFormVo courseFormVo = courseService.getCourseInfoById(id);
        return Result.ok(courseFormVo);
    }

    /**
     * 修改课程基本信息
     * @param courseFormVo
     * @return
     */
    @ApiOperation("修改课程基本信息")
    @PutMapping("course")
    public Result updateCourseInfo(@RequestBody CourseFormVo courseFormVo) {
        Long courseId = courseService.updateCourseInfo(courseFormVo);
        return Result.ok(courseId);
    }

    /**
     * 根据id获取发布课程信息
     * @param id
     * @return
     */
    @ApiOperation("根据id获取发布课程信息")
    @GetMapping("course_publish/{id}")
    public Result publish(@PathVariable Long id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVo(id);
        return Result.ok(coursePublishVo);
    }

    /**
     * 课程最终发布
     * @param id 课程id
     * @return
     */
    @ApiOperation("课程最终发布")
    @PutMapping("course_publish/{id}")
    public Result finalPublish(@PathVariable Long id) {
        courseService.finalPublishCourse(id);
        return Result.ok();
    }

    /**
     * 根据id删除课程
     * @param id 课程id
     * @return
     */
    @ApiOperation("根据id删除课程")
    @DeleteMapping("course/{id}")
    public Result delete(@PathVariable Long id) {
        courseService.deleteCourseById(id);
        return Result.ok();
    }

}

